<?php session_start(); //Session ?>
<?php include_once( "_db.php" ); //Database ?>
<?php include_once( "funcdef.php" ); //Functions Definition ?>
<?php include_once( "header.php" ); //Header ?>
<?php include_once( "footer.php" ); //Footer ?>
<?php
//include page header
header_template("Frontage", $_SERVER['PHP_SELF']."?pid=".$_GET["pid"], "icon-tag");

//db OBJECT invoked!
$db = new DATABASE();

$bag_page_href = "bag.php";
?>
      <div class="row">
        <div class="span6">
<?php
if (isset($_GET["pid"])) {
  //construct SELECT query
  $conds = array();
  $param_col = array(0);
  $param_op  = array(0);
  $param_key = array($_GET["pid"]);
  $conds[] = $param_col;
  $conds[] = $param_op;
  $conds[] = $param_key;
  $query = $db->db_select("product", NULL, $conds);
  //execute SELECT query
  $result = $db->database->query($query);
  //result SELECT query
  $resProd = $result->fetchAll(SQLITE_ASSOC);

  if (empty($resProd)) {
    $error_msg = "Product not exist.";
    printAlert("error", "Oh snap!", $error_msg, "#");
  } else {
  
    if(!isset($_SESSION['shoppingBag'])) {
      $_SESSION['shoppingBag'] = array();
    }

    $err = bag_operation($db, $_GET["action"], $_GET["pid"]);
    if ($err != "") {
      printAlert("error", "Oh snap!", $err, $_SERVER['PHP_SELF']."?pid=".$_GET["pid"]);
    }

    $bagProd = array();
    $bagTotItem = 0;
    $bagProd = bag_construction($db);
    sort($_SESSION['shoppingBag']);
    $bagUnique = array_unique($_SESSION['shoppingBag']);
    $bagCount = array_count_values($_SESSION['shoppingBag']);

    $data_content  = "\n";
    $data_content .= "<table class='table table-condensed'>\n";
    $data_content .= "<tbody>\n";

    foreach($bagProd as $value) {
      if (in_array($value['product_id'], $bagUnique)) {
        $bagTotItem++;
        $bagPrice[$value['product_id']] = $bagCount[$value['product_id']] * $value['product_price'];
        if ($value['picture_name'] != "") {
          $img_src_resize = "timthumb.php?src=".$value['picture_name']."&w=60&h=45&a=t";
        } else {
          $img_src_resize = "http://placehold.it/60x45";
        }
          $data_content .= "<tr>\n";
          $data_content .= "<td><img src='".$img_src_resize."'></td>\n";
          $data_content .= "<td>\n";
          $data_content .= "<span class='label label-default'>MYR ".$value['product_price']."</span>\n";
          $data_content .= "<span class='label label-info'>x".$bagCount[$value['product_id']]."</span>\n";
          //$data_content .= "<br />\n";
          $data_content .= "<span class='label label-success'>MYR ".sprintf("%01.2f", ($value['product_price']*$bagCount[$value['product_id']]))."</span>\n";
          $data_content .= "</td>\n";
          $data_content .= "</tr>\n";
      }
    }
    $data_content .= "<tr>\n";
    $data_content .= "<td>&nbsp;</td>\n";
    $data_content .= "<td>\n";
    $data_content .= "<span class='label label-inverse'>TOTAL</span>&nbsp;";
    if (isset($bagPrice)) {
      $totPrice = array_sum($bagPrice);
    } else {
      $totPrice = 0;
    }
    $data_content .= "<span class='label label-success'>MYR ".sprintf("%01.2f", $totPrice)."</span>";
    $data_content .= "</td>\n";
    $data_content .= "</tr>\n";
    $data_content .= "</tbody>\n";
    $data_content .= "</table>\n";

    //construct SELECT query
    $cols = array(1);
    $conds = array();
    $param_col = array(2);
    $param_op= array(0);
    $param_key = array($_GET["pid"]);
    $conds[] = $param_col;
    $conds[] = $param_op;
    $conds[] = $param_key;
    $query = $db->db_select("picture2product", $cols, $conds);
    //execute SELECT query
    $result = $db->database->query($query);
    //result SELECT query
    $resPic2Prod = $result->fetchAll(SQLITE_ASSOC);

    //construct SELECT query
    $cols = array(1);
    $conds = array();
    $param_col = array(2);
    $param_op= array(0);
    $param_key = array($_GET["pid"]);
    $conds[] = $param_col;
    $conds[] = $param_op;
    $conds[] = $param_key;
    $query = $db->db_select("category2product", $cols, $conds);
    //execute SELECT query
    $result = $db->database->query($query);
    //result SELECT query
    $resCat2Prod = $result->fetchAll(SQLITE_ASSOC);
?>
          <div class="pull-right">
            <a href="<?php echo $bag_page_href; ?>" rel="popover" title="Bag" data-content="<?php echo $data_content; ?>">
              <img src="pic/glyphicons/png/glyphicons_350_shopping_bag.png"> <?php echo $bagTotItem; ?> item(s) in bag
            </a>
          </div>
          <div class="page-header">
            <h3><?php echo $resProd[0]['product_name']; ?></h3>
          </div>
          <ul class="thumbnails">
<?php
    if ($resProd[0]['picture_id'] != 0) {
      //construct SELECT query
      $cols = array(1);
      $conds = array();
      $param_col = array(0);
      $param_op  = array(0);
      $param_key = array($resProd[0]['picture_id']);
      $conds[] = $param_col;
      $conds[] = $param_op;
      $conds[] = $param_key;
      $query = $db->db_select("picture", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resPic = $result->fetchAll(SQLITE_ASSOC);
      $img_src = $resPic[0]['picture_name'];
      $img_src_resize = "timthumb.php?src=".$img_src."&w=360&h=268&a=t";
?>
            <li class="span4">
              <a href="<?php echo $img_src; ?>" class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
              </a>
            </li>
<?php
    } else {
      $img_src = "http://placehold.it/";
      $img_src_resize = $img_src."360x268&text=no+pic(s)";
?>
            <li class="span4">
              <div class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
              </div>
            </li>
<?php
    }
     $picCount = 0;
     foreach ($resPic2Prod as $key => $value) {
       if ($resPic2Prod[$key]['picture_id'] != $resProd[0]['picture_id']) {
        $picCount++;
        //construct SELECT query
        $cols = array(1);
        $conds = array();
        $param_col = array(0);
        $param_op  = array(0);
        $param_key = array($resPic2Prod[$key]['picture_id']);
        $conds[] = $param_col;
        $conds[] = $param_op;
        $conds[] = $param_key;
        $query = $db->db_select("picture", $cols, $conds);
        //execute SELECT query
        $result = $db->database->query($query);
        //result SELECT query
        $resPic = $result->fetchAll(SQLITE_ASSOC);
        $img_src = $resPic[0]['picture_name'];
        $img_src_resize = "timthumb.php?src=".$img_src."&w=160&h=120&a=t";
?>
            <li class="span2">
              <a href="<?php echo $img_src; ?>" class="thumbnail">
                <img src="<?php echo $img_src_resize; ?>">
              </a>
            </li>
<?php
      } else {
        //do nothing
      }
    }
    for ($i = $picCount; $i < 5; $i++) {
?>
            <li class="span2">
              <div class="thumbnail">
                <img src="http://placehold.it/160x120&text=no+pic(s)">
              </div>
            </li>
<?php
    }
    if ($resProd[0]['product_quantity'] > 0) {
      if (!in_array($_GET["pid"], $bagUnique)) {
        $add_to_bag_btn_href    = " href='".$_SERVER['PHP_SELF']."?action=add&pid=".$_GET["pid"]."'";
        $add_to_bag_btn_disable = "";
        $add_to_bag_btn_txt     = "Add to Bag";
      } else {
        $add_to_bag_btn_href    = "";
        $add_to_bag_btn_disable = " disabled";
        $add_to_bag_btn_txt     = "In Bag";
      }
    } else {
      $add_to_bag_btn_href    = "";
      $add_to_bag_btn_disable = " disabled";
      $add_to_bag_btn_txt     = "Out of Stock";
    }
?>
          </ul>
          <hr />
          <div class="row">
            <div class="span2">
              <p><a class="btn btn-large btn-info<?php echo $add_to_bag_btn_disable; ?>"<?php echo $add_to_bag_btn_href; ?>>&nbsp;<i class="icon-briefcase icon-white"></i>&nbsp;<?php echo $add_to_bag_btn_txt; ?>&nbsp;</a></p>
              <h5><i class="icon-ok-sign"></i> MYR <?php echo $resProd[0]['product_price']; ?></h5>
              <h5><i class="icon-ok-sign"></i> <?php echo $resProd[0]['product_quantity']; ?> piece(s) left</h5>
              <a href="https://twitter.com/share" class="twitter-share-button" data-text="<?php echo $resProd[0]['product_name']; ?>" data-via="amischaheera" data-related="lumachroma" data-hashtags="stylemode">Tweet</a>
              <!--<a href="https://twitter.com/share" class="twitter-share-button" data-text="<?php echo $resProd[0]['product_name']; ?>" data-via="amischaheera" data-lang="en" data-hashtags="stylemode" data-count="vertical">Tweet</a>-->
              <br />
              <div class="fb-like" data-href="http://stick.sptnkswthrt.com<?php echo $_SERVER['PHP_SELF']."?pid=".$_GET["pid"]; ?>" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false" data-font="trebuchet ms"></div>
              <!--<div class="fb-like" data-href="http://stick.sptnkswthrt.com<?php echo $_SERVER['PHP_SELF']."?pid=".$_GET["pid"]; ?>" data-send="false" data-layout="box_count" data-width="55" data-show-faces="false" data-font="trebuchet ms"></div>-->
              <br />
              <div class="g-plusone" data-size="medium" data-href="http://stick.sptnkswthrt.com<?php echo $_SERVER['PHP_SELF']."?pid=".$_GET["pid"]; ?>"></div>
              <!--<div class="g-plusone" data-size="tall" data-href="http://stick.sptnkswthrt.com<?php echo $_SERVER['PHP_SELF']."?pid=".$_GET["pid"]; ?>"></div>-->
            </div>
            <div class="span4">
              <div class="well">
                <p><?php echo $resProd[0]['product_description']; ?></p>
                <p>
                  <span class="label label-info">Category</span>
<?php
    foreach ($resCat2Prod as $key => $value) {
      //construct SELECT query
      $cols = array(1);
      $conds = array();
      $param_col = array(0);
      $param_op  = array(0);
      $param_key = array($resCat2Prod[$key]['category_id']);
      $conds[] = $param_col;
      $conds[] = $param_op;
      $conds[] = $param_key;
      $query = $db->db_select("category", $cols, $conds);
      //execute SELECT query
      $result = $db->database->query($query);
      //result SELECT query
      $resCat = $result->fetchAll(SQLITE_ASSOC);
?>
                  <span class="label label-default"><?php echo $resCat[0]['category_name']; ?></span>
<?php
    }
?>
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br />
      <script>
        (function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) return;
          js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
      </script>
      <script>
        !function(d,s,id){
          var js,fjs=d.getElementsByTagName(s)[0];
          if(!d.getElementById(id)){
            js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";
            fjs.parentNode.insertBefore(js,fjs);
          }
        }(document,"script","twitter-wjs");
      </script>
      <script type="text/javascript">
        (function() {
          var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
          po.src = 'https://apis.google.com/js/plusone.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
        })();
      </script>
<?php
  }
} else {
  $error_msg = "Page error.";
  printAlert("error", "Oh snap!", $error_msg, "#");
}
//include page footer
footer_template();
?>
